home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / PEAR / Command / Install.php < prev    next >
PHP Script  |  2004-10-01  |  18KB  |  466 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Stig Sµther Bakken <ssb@php.net>                             |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Install.php,v 1.52 2004/01/08 17:33:13 sniper Exp $
  20.  
  21. require_once "PEAR/Command/Common.php";
  22. require_once "PEAR/Installer.php";
  23.  
  24. /**
  25.  * PEAR commands for installation or deinstallation/upgrading of
  26.  * packages.
  27.  *
  28.  */
  29. class PEAR_Command_Install extends PEAR_Command_Common
  30. {
  31.     // {{{ properties
  32.  
  33.     var $commands = array(
  34.         'install' => array(
  35.             'summary' => 'Install Package',
  36.             'function' => 'doInstall',
  37.             'shortcut' => 'i',
  38.             'options' => array(
  39.                 'force' => array(
  40.                     'shortopt' => 'f',
  41.                     'doc' => 'will overwrite newer installed packages',
  42.                     ),
  43.                 'nodeps' => array(
  44.                     'shortopt' => 'n',
  45.                     'doc' => 'ignore dependencies, install anyway',
  46.                     ),
  47.                 'register-only' => array(
  48.                     'shortopt' => 'r',
  49.                     'doc' => 'do not install files, only register the package as installed',
  50.                     ),
  51.                 'soft' => array(
  52.                     'shortopt' => 's',
  53.                     'doc' => 'soft install, fail silently, or upgrade if already installed',
  54.                     ),
  55.                 'nobuild' => array(
  56.                     'shortopt' => 'B',
  57.                     'doc' => 'don\'t build C extensions',
  58.                     ),
  59.                 'nocompress' => array(
  60.                     'shortopt' => 'Z',
  61.                     'doc' => 'request uncompressed files when downloading',
  62.                     ),
  63.                 'installroot' => array(
  64.                     'shortopt' => 'R',
  65.                     'arg' => 'DIR',
  66.                     'doc' => 'root directory used when installing files (ala PHP\'s INSTALL_ROOT)',
  67.                     ),
  68.                 'ignore-errors' => array(
  69.                     'doc' => 'force install even if there were errors',
  70.                     ),
  71.                 'alldeps' => array(
  72.                     'shortopt' => 'a',
  73.                     'doc' => 'install all required and optional dependencies',
  74.                     ),
  75.                 'onlyreqdeps' => array(
  76.                     'shortopt' => 'o',
  77.                     'doc' => 'install all required dependencies',
  78.                     ),
  79.                 ),
  80.             'doc' => '<package> ...
  81. Installs one or more PEAR packages.  You can specify a package to
  82. install in four ways:
  83.  
  84. "Package-1.0.tgz" : installs from a local file
  85.  
  86. "http://example.com/Package-1.0.tgz" : installs from
  87. anywhere on the net.
  88.  
  89. "package.xml" : installs the package described in
  90. package.xml.  Useful for testing, or for wrapping a PEAR package in
  91. another package manager such as RPM.
  92.  
  93. "Package" : queries your configured server
  94. ({config master_server}) and downloads the newest package with
  95. the preferred quality/state ({config preferred_state}).
  96.  
  97. More than one package may be specified at once.  It is ok to mix these
  98. four ways of specifying packages.
  99. '),
  100.         'upgrade' => array(
  101.             'summary' => 'Upgrade Package',
  102.             'function' => 'doInstall',
  103.             'shortcut' => 'up',
  104.             'options' => array(
  105.                 'force' => array(
  106.                     'shortopt' => 'f',
  107.                     'doc' => 'overwrite newer installed packages',
  108.                     ),
  109.                 'nodeps' => array(
  110.                     'shortopt' => 'n',
  111.                     'doc' => 'ignore dependencies, upgrade anyway',
  112.                     ),
  113.                 'register-only' => array(
  114.                     'shortopt' => 'r',
  115.                     'doc' => 'do not install files, only register the package as upgraded',
  116.                     ),
  117.                 'nobuild' => array(
  118.                     'shortopt' => 'B',
  119.                     'doc' => 'don\'t build C extensions',
  120.                     ),
  121.                 'nocompress' => array(
  122.                     'shortopt' => 'Z',
  123.                     'doc' => 'request uncompressed files when downloading',
  124.                     ),
  125.                 'installroot' => array(
  126.                     'shortopt' => 'R',
  127.                     'arg' => 'DIR',
  128.                     'doc' => 'root directory used when installing files (ala PHP\'s INSTALL_ROOT)',
  129.                     ),
  130.                 'ignore-errors' => array(
  131.                     'doc' => 'force install even if there were errors',
  132.                     ),
  133.                 'alldeps' => array(
  134.                     'shortopt' => 'a',
  135.                     'doc' => 'install all required and optional dependencies',
  136.                     ),
  137.                 'onlyreqdeps' => array(
  138.                     'shortopt' => 'o',
  139.                     'doc' => 'install all required dependencies',
  140.                     ),
  141.                 ),
  142.             'doc' => '<package> ...
  143. Upgrades one or more PEAR packages.  See documentation for the
  144. "install" command for ways to specify a package.
  145.  
  146. When upgrading, your package will be updated if the provided new
  147. package has a higher version number (use the -f option if you need to
  148. upgrade anyway).
  149.  
  150. More than one package may be specified at once.
  151. '),
  152.         'upgrade-all' => array(
  153.             'summary' => 'Upgrade All Packages',
  154.             'function' => 'doInstall',
  155.             'shortcut' => 'ua',
  156.             'options' => array(
  157.                 'nodeps' => array(
  158.                     'shortopt' => 'n',
  159.                     'doc' => 'ignore dependencies, upgrade anyway',
  160.                     ),
  161.                 'register-only' => array(
  162.                     'shortopt' => 'r',
  163.                     'doc' => 'do not install files, only register the package as upgraded',
  164.                     ),
  165.                 'nobuild' => array(
  166.                     'shortopt' => 'B',
  167.                     'doc' => 'don\'t build C extensions',
  168.                     ),
  169.                 'nocompress' => array(
  170.                     'shortopt' => 'Z',
  171.                     'doc' => 'request uncompressed files when downloading',
  172.                     ),
  173.                 'installroot' => array(
  174.                     'shortopt' => 'R',
  175.                     'arg' => 'DIR',
  176.                     'doc' => 'root directory used when installing files (ala PHP\'s INSTALL_ROOT)',
  177.                     ),
  178.                 'ignore-errors' => array(
  179.                     'doc' => 'force install even if there were errors',
  180.                     ),
  181.                 ),
  182.             'doc' => '
  183. Upgrades all packages that have a newer release available.  Upgrades are
  184. done only if there is a release available of the state specified in
  185. "preferred_state" (currently {config preferred_state}), or a state considered
  186. more stable.
  187. '),
  188.         'uninstall' => array(
  189.             'summary' => 'Un-install Package',
  190.             'function' => 'doUninstall',
  191.             'shortcut' => 'un',
  192.             'options' => array(
  193.                 'nodeps' => array(
  194.                     'shortopt' => 'n',
  195.                     'doc' => 'ignore dependencies, uninstall anyway',
  196.                     ),
  197.                 'register-only' => array(
  198.                     'shortopt' => 'r',
  199.                     'doc' => 'do not remove files, only register the packages as not installed',
  200.                     ),
  201.                 'installroot' => array(
  202.                     'shortopt' => 'R',
  203.                     'arg' => 'DIR',
  204.                     'doc' => 'root directory used when installing files (ala PHP\'s INSTALL_ROOT)',
  205.                     ),
  206.                 'ignore-errors' => array(
  207.                     'doc' => 'force install even if there were errors',
  208.                     ),
  209.                 ),
  210.             'doc' => '<package> ...
  211. Uninstalls one or more PEAR packages.  More than one package may be
  212. specified at once.
  213. '),
  214.         'bundle' => array(
  215.             'summary' => 'Unpacks a Pecl Package',
  216.             'function' => 'doBundle',
  217.             'shortcut' => 'bun',
  218.             'options' => array(
  219.                 'destination' => array(
  220.                    'shortopt' => 'd',
  221.                     'arg' => 'DIR',
  222.                     'doc' => 'Optional destination directory for unpacking (defaults to current path or "ext" if exists)',
  223.                     ),
  224.                 'force' => array(
  225.                     'shortopt' => 'f',
  226.                     'doc' => 'Force the unpacking even if there were errors in the package',
  227.                 ),
  228.             ),
  229.             'doc' => '<package>
  230. Unpacks a Pecl Package into the selected location. It will download the
  231. package if needed.
  232. '),
  233.     );
  234.  
  235.     // }}}
  236.     // {{{ constructor
  237.  
  238.     /**
  239.      * PEAR_Command_Install constructor.
  240.      *
  241.      * @access public
  242.      */
  243.     function PEAR_Command_Install(&$ui, &$config)
  244.     {
  245.         parent::PEAR_Command_Common($ui, $config);
  246.     }
  247.  
  248.     // }}}
  249.  
  250.     // {{{ doInstall()
  251.  
  252.     function doInstall($command, $options, $params)
  253.     {
  254.         require_once 'PEAR/Downloader.php';
  255.         if (empty($this->installer)) {
  256.             $this->installer = &new PEAR_Installer($this->ui);
  257.         }
  258.         if ($command == 'upgrade') {
  259.             $options['upgrade'] = true;
  260.         }
  261.         if ($command == 'upgrade-all') {
  262.             include_once "PEAR/Remote.php";
  263.             $options['upgrade'] = true;
  264.             $remote = &new PEAR_Remote($this->config);
  265.             $state = $this->config->get('preferred_state');
  266.             if (empty($state) || $state == 'any') {
  267.                 $latest = $remote->call("package.listLatestReleases");
  268.             } else {
  269.                 $latest = $remote->call("package.listLatestReleases", $state);
  270.             }
  271.             if (PEAR::isError($latest)) {
  272.                 return $latest;
  273.             }
  274.             $reg = new PEAR_Registry($this->config->get('php_dir'));
  275.             $installed = array_flip($reg->listPackages());
  276.             $params = array();
  277.             foreach ($latest as $package => $info) {
  278.                 $package = strtolower($package);
  279.                 if (!isset($installed[$package])) {
  280.                     // skip packages we don't have installed
  281.                     continue;
  282.                 }
  283.                 $inst_version = $reg->packageInfo($package, 'version');
  284.                 if (version_compare("$info[version]", "$inst_version", "le")) {
  285.                     // installed version is up-to-date
  286.                     continue;
  287.                 }
  288.                 $params[] = $package;
  289.                 $this->ui->outputData(array('data' => "Will upgrade $package"), $command);
  290.             }
  291.         }
  292.         $this->downloader = &new PEAR_Downloader($this->ui, $options, $this->config);
  293.         $errors = array();
  294.         $downloaded = array();
  295.         $this->downloader->download($params);
  296.         $errors = $this->downloader->getErrorMsgs();
  297.         if (count($errors)) {
  298.             $err['data'] = array($errors);
  299.             $err['headline'] = 'Install Errors';
  300.             $this->ui->outputData($err);
  301.             return $this->raiseError("$command failed");
  302.         }
  303.         $downloaded = $this->downloader->getDownloadedPackages();
  304.         $this->installer->sortPkgDeps($downloaded);
  305.         foreach ($downloaded as $pkg) {
  306.             $bn = basename($pkg['file']);
  307.             $info = $this->installer->install($pkg['file'], $options, $this->config);
  308.             if (is_array($info)) {
  309.                 if ($this->config->get('verbose') > 0) {
  310.                     $label = "$info[package] $info[version]";
  311.                     $out = array('data' => "$command ok: $label");
  312.                     if (isset($info['release_warnings'])) {
  313.                         $out['release_warnings'] = $info['release_warnings'];
  314.                     }
  315.                     $this->ui->outputData($out, $command);
  316.                 }
  317.             } else {
  318.                 return $this->raiseError("$command failed");
  319.             }
  320.         }
  321.         return true;
  322.     }
  323.  
  324.     // }}}
  325.     // {{{ doUninstall()
  326.  
  327.     function doUninstall($command, $options, $params)
  328.     {
  329.         if (empty($this->installer)) {
  330.             $this->installer = &new PEAR_Installer($this->ui);
  331.         }
  332.         if (sizeof($params) < 1) {
  333.             return $this->raiseError("Please supply the package(s) you want to uninstall");
  334.         }
  335.         include_once 'PEAR/Registry.php';
  336.         $reg = new PEAR_Registry($this->config->get('php_dir'));
  337.         $newparams = array();
  338.         $badparams = array();
  339.         foreach ($params as $pkg) {
  340.             $info = $reg->packageInfo($pkg);
  341.             if ($info === null) {
  342.                 $badparams[] = $pkg;
  343.             } else {
  344.                 $newparams[] = $info;
  345.             }
  346.         }
  347.         $this->installer->sortPkgDeps($newparams, true);
  348.         $params = array();
  349.         foreach($newparams as $info) {
  350.             $params[] = $info['info']['package'];
  351.         }
  352.         $params = array_merge($params, $badparams);
  353.         foreach ($params as $pkg) {
  354.             if ($this->installer->uninstall($pkg, $options)) {
  355.                 if ($this->config->get('verbose') > 0) {
  356.                     $this->ui->outputData("uninstall ok: $pkg", $command);
  357.                 }
  358.             } else {
  359.                 return $this->raiseError("uninstall failed: $pkg");
  360.             }
  361.         }
  362.         return true;
  363.     }
  364.  
  365.     // }}}
  366.  
  367.  
  368.     // }}}
  369.     // {{{ doBundle()
  370.     /*
  371.     (cox) It just downloads and untars the package, does not do
  372.             any check that the PEAR_Installer::_installFile() does.
  373.     */
  374.  
  375.     function doBundle($command, $options, $params)
  376.     {
  377.         if (empty($this->installer)) {
  378.             $this->installer = &new PEAR_Downloader($this->ui);
  379.         }
  380.         $installer = &$this->installer;
  381.         if (sizeof($params) < 1) {
  382.             return $this->raiseError("Please supply the package you want to bundle");
  383.         }
  384.         $pkgfile = $params[0];
  385.         $need_download = false;
  386.         if (preg_match('#^(http|ftp)://#', $pkgfile)) {
  387.             $need_download = true;
  388.         } elseif (!@is_file($pkgfile)) {
  389.             if ($installer->validPackageName($pkgfile)) {
  390.                 $pkgfile = $installer->getPackageDownloadUrl($pkgfile);
  391.                 $need_download = true;
  392.             } else {
  393.                 if (strlen($pkgfile)) {
  394.                     return $this->raiseError("Could not open the package file: $pkgfile");
  395.                 } else {
  396.                     return $this->raiseError("No package file given");
  397.                 }
  398.             }
  399.         }
  400.  
  401.         // Download package -----------------------------------------------
  402.         if ($need_download) {
  403.             $downloaddir = $installer->config->get('download_dir');
  404.             if (empty($downloaddir)) {
  405.                 if (PEAR::isError($downloaddir = System::mktemp('-d'))) {
  406.                     return $downloaddir;
  407.                 }
  408.                 $installer->log(2, '+ tmp dir created at ' . $downloaddir);
  409.             }
  410.             $callback = $this->ui ? array(&$installer, '_downloadCallback') : null;
  411.             $file = $installer->downloadHttp($pkgfile, $this->ui, $downloaddir, $callback);
  412.             if (PEAR::isError($file)) {
  413.                 return $this->raiseError($file);
  414.             }
  415.             $pkgfile = $file;
  416.         }
  417.  
  418.        // Parse xml file -----------------------------------------------
  419.         $pkginfo = $installer->infoFromTgzFile($pkgfile);
  420.         if (PEAR::isError($pkginfo)) {
  421.             return $this->raiseError($pkginfo);
  422.         }
  423.         $installer->validatePackageInfo($pkginfo, $errors, $warnings);
  424.         // XXX We allow warnings, do we have to do it?
  425.         if (count($errors)) {
  426.              if (empty($options['force'])) {
  427.                 return $this->raiseError("The following errors where found:\n".
  428.                                                  implode("\n", $errors));
  429.             } else {
  430.                 $this->log(0, "warning : the following errors were found:\n".
  431.                            implode("\n", $errors));
  432.             }
  433.         }
  434.         $pkgname = $pkginfo['package'];
  435.  
  436.         // Unpacking -------------------------------------------------
  437.  
  438.         if (isset($options['destination'])) {
  439.             if (!is_dir($options['destination'])) {
  440.                 System::mkdir('-p ' . $options['destination']);
  441.             }
  442.             $dest = realpath($options['destination']);
  443.         } else {
  444.             $pwd = getcwd();
  445.             if (is_dir($pwd . DIRECTORY_SEPARATOR . 'ext')) {
  446.                 $dest = $pwd . DIRECTORY_SEPARATOR . 'ext';
  447.             } else {
  448.                 $dest = $pwd;
  449.             }
  450.         }
  451.         $dest .= DIRECTORY_SEPARATOR . $pkgname;
  452.         $orig = $pkgname . '-' . $pkginfo['version'];
  453.  
  454.         $tar = new Archive_Tar($pkgfile);
  455.         if (!@$tar->extractModify($dest, $orig)) {
  456.             return $this->raiseError("unable to unpack $pkgfile");
  457.         }
  458.         $this->ui->outputData("Package ready at '$dest'");
  459.     // }}}
  460.     }
  461.  
  462.     // }}}
  463.  
  464. }
  465. ?>
  466.